<?php

declare(strict_types=1);

namespace app\admin\service\file;

use app\common\model\file\FileModel;
use Dengje\Pxy\BaseService;

class FileGroupService extends BaseService
{
    protected $model = null;


    public function __construct()
    {

        $this->model = new \app\common\model\file\FileGroupModel();
    }

    public function list($mediaType = 'image')
    {
        if ($mediaType == 'image') {

            // 定义图片MIME类型数组
            $mimeTypes = [
                'image/jpeg',
                'image/jpg',
                'image/png',
                'image/gif',
                'image/bmp',
                'image/webp',
                'image/tiff',
                'image/x-tiff',
                'image/x-icon',
                'image/vnd.microsoft.icon',
                'image/x-adobe-dng',
                // 可以继续添加其他图片MIME类型
            ];
            $where[] = ['mimetype', 'in', $mimeTypes];
        } elseif ($mediaType == 'video') {
            $mimeTypes = [
                'video/mp4',
                'video/x-msvideo',
                'video/x-ms-wmv',
                'video/quicktime',
                'video/3gpp',
                'video/3gpp2',
                'video/x-flv',
                'video/webm',
                'video/x-matroska',
                'video/ogg',
                'application/octet-stream',
                // 可以继续添加其他视频MIME类型
            ];
            $where[] = ['mimetype', 'in', $mimeTypes];
        } elseif ($mediaType == 'file') {
            $mimeTypes = [
                'text/plain',
                'text/html',
                'text/css',
                'application/javascript',
                'application/xml',
                'application/pdf',
                'audio/mpeg'
            ];
            $where[] = ['mimetype', 'in', $mimeTypes];
        }
        if (request()->has('page_size')) {
            $list = $this->model->where('media_type', $mediaType)->order(['sort' => 'asc'])->order(['id'=>'desc'])->paginate(request()->get('page_size/d',10));
        } else {
            $list = $this->model->where('media_type', $mediaType)->order(['sort' => 'asc'])->select()->toArray();
            for ($i = 0; $i < count($list); $i++) {
                $list[$i]['count'] = FileModel::where($where)->where('group_id', $list[$i]['id'])->count();
            }
            array_unshift($list, [
                'id' => 0,
                'name' => '全部',
                'media_type' => $mediaType,
                'count' => FileModel::where($where)->count()
            ]);
        }
        return $list;
    }

    public function create($data)
    {

        return $this->model->create($data);
    }

    public function update($id, $data)
    {
        $data = $this->preExcludeFields($data);
        return $this->model->where('id', $id)->update($data);
    }

    public function detail($id)
    {
        return $this->model->where('id', $id)->find();
    }

    public function delete($id)
    {
        return $this->model->where('id', $id)->delete();
    }
}
